有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java托管Bean不工作:调用NotingMB文件中的save函数时出现空指针异常

我正在使用Eclipse以及jsf和MVC框架。。当保存来自Xhtml文件的bean文件调用函数时。。它显示错误:notingMB上出现空指针异常。拯救

代码: “xhtml文件” “CreateFileDialog.xhtml”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:body>
    <h:form id="CreateFileDialogForm" prependId="false">
    <p:dialog widgetVar="CreateFileDialogWidget"
        id="CreateFileDialogId" height="500" width="500" modal="true"
        closable="true" draggable="true" resizable="false" header="Create File">


            <h:panelGrid columns="2" cellpadding="5">

            <h:outputLabel for="Subject" value="#{bundle.subject}"/>
                <h:inputText id="Subject" value="#{fileMB.info.subject}" required="true"/>   

                <h:outputLabel for="fileRefNo" value="File-Ref-No"/>
                <h:inputText id="fileRefNo" value="#{fileMB.info.fileRefNo}" required="true"/>   



                 <h:outputText value="Classification" />
                    <p:selectOneRadio id="Classi" value="#{fileMB.info.classification}">
                    <f:selectItem itemLabel="Normal" itemValue="Normal" />
                    <f:selectItem itemLabel="Restricted" itemValue="Restricted" />
                    <f:selectItem itemLabel="Confidential" itemValue="Confidential" />
                    <f:selectItem itemLabel="Secret" itemValue="Secret" />
                    <f:selectItem itemLabel="TopSecret" itemValue="Top Secret" />
                    </p:selectOneRadio>

                    <h:outputText value="Priority" />
                    <p:selectOneRadio id="prior" value="#{fileMB.info.priority}">
                    <f:selectItem itemLabel="Normal" itemValue="Normal" />
                    <f:selectItem itemLabel="High" itemValue="High" />
                    <f:selectItem itemLabel="Medium" itemValue="Medium" />
                    <f:selectItem itemLabel="Low" itemValue="Low" />
                    </p:selectOneRadio>
                 <h:outputText for="Description" value="#{bundle.briefDescription}" />
                 <p:inputTextarea id="Description" value="#{fileMB.info.description}" rows="5" cols="30" counter="display" maxlength="10" counterTemplate="{0} characters remaining." autoResize="false" />
                 <h:outputText id="display" />
                 <br></br>

                 <p:commandButton value="Add Comment" icon="ui-icon-plus"
                         onclick="CreateComentDialogWidget.show();"/> 
                 <br></br>

                 <br></br>
                <p:commandButton value="create" actionListener="#{fileMB.save}" />
                <p:commandButton value="cancel" icon="ui-icon-cancel"
                    onclick="CreateFileDialogWidget.hide();" type="button" />

            </h:panelGrid>

    </p:dialog>
    </h:form>
    <ui:include src="/pages/protected/filling/File/CreateComentDialog.xhtml" />

</h:body>
</html>

“CreateCommentDialog.xhtml”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:body>
    <h:form id="CreateComentDialogForm" prependId="false">
    <p:dialog widgetVar="CreateComentDialogWidget"
        id="CreateComentDialogId" height="500" width="500" modal="true"
        closable="true" draggable="true" resizable="false" header="Create File">


            <h:panelGrid columns="2" cellpadding="5">
            <h:outputLabel for="coment" value="write coment"/>
               <p:inputTextarea id="coment" value="#{notingMB.note.comment}" rows="5" cols="30" counter="display2" maxlength="50" counterTemplate="{0} characters remaining." autoResize="false" />
                 <h:outputText id="display2" />
                 <br></br>

                   <p:commandButton value="create" actionListener="#{notingMB.save}" />
                   <p:commandButton value="cancel" icon="ui-icon-cancel"
                    onclick="CreateComentDialogWidget.hide();" type="button" />

               </h:panelGrid>
               </p:dialog>
               </h:form>

               </h:body>
               </html>

“BEAN文件” “FileMB.java”

package com.managedBean.Filling;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.primefaces.model.CheckboxTreeNode;
import org.primefaces.model.TreeNode;

import com.controller.Controller;
import com.dataModel.filling.FileDataModel;
import com.dataModel.filling.HeadingDataModel;
import com.managedBean.BaseMB;
import com.model.um.Organization;
import com.model.um.OrganizationType;
import com.model.um.Person;
import com.model.filling.File;
import com.model.filling.Heading;
import com.model.filling.Noting;

import org.primefaces.event.SelectEvent;
@ManagedBean(name="fileMB")
@SessionScoped
public class FileMB extends BaseMB implements Serializable
{
    public static final long serialVersionUID = 1L;
    public static final Logger log = LogManager.getLogger(FileMB.class.getName());

    private File info;
    private File file;
    private Person person;
    private List<Person> personList;
    private FileDataModel fileDataModel;
    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Person> getPersonList() {
        this.personList = controller.getAllPersons();
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }




    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public File getInfo() {
        return info;
    }

    public void setInfo(File info) {
        this.info = info;
    }
    public FileDataModel getFileDataModel() {
        List<File> fileList = controller.getAllFiles();
        this.fileDataModel = new FileDataModel(fileList);

        return fileDataModel;
    }

    public void setFileDataModel(FileDataModel fileDataModel) {
        this.fileDataModel = fileDataModel;
    }

    public FileMB()
    {
        this.info = new File();
        this.person = new Person();
        this.file = new File();
        controller = Controller.getInstance();
    }
    public void resetPerson()
    {
        this.person = new Person();
        System.out.println("reset...");
    }
    public void resetFile()
    {
        this.file = new File();
        System.out.println("reset...");
    }


    public void save()
    {

        System.out.println(info.getSubject());
        controller.saveFileInfo(info);
        System.out.println("before save");

    }

}

“NotingMB.java”

package com.managedBean.Filling;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.controller.Controller;
import com.managedBean.BaseMB;
import com.model.filling.Noting;
@ManagedBean(name="notingMB")
@SessionScoped
public class NotingMB extends BaseMB implements Serializable
{
    public static final long serialVersionUID = 1L;
    public static final Logger log = LogManager.getLogger(NotingMB.class.getName());

    private Noting note;




    public Noting getNote() {
        return note;
    }

    public void setNote(Noting note) {
        this.note = note;
    }



    public NotingMB()
    {

        this.note = new Noting();

        //controller = Controller.getInstance();
    }

    public void save()
    {

        System.out.println("before save");
        System.out.println(note.getComment());
        controller.saveComment(note);
        System.out.println("after save");
    }
}

共 (1) 个答案

  1. # 1 楼答案

    actionListener属性将接受javax。面孔。事件ActionEvent参数

    你应该改变

    public void save() {
    
        System.out.println("before save");
        System.out.println(note.getComment());
        controller.saveComment(note);
        System.out.println("after save");
    }
    

    public void save(ActionEvent event) {
    
        System.out.println("before save");
        System.out.println(note.getComment());
        controller.saveComment(note);
        System.out.println("after save");
    }